var QuoteApi = require('../../../api/quote.js');
var util = require('../../../utils/util');
var {metalList, metalList_daily, metalList_weekly} = require('../../utils/metalList.js');
var {LMEStockColumns, LMECancelledColumns, COMEXColumns, SHFEDailyColumn, SHFEWeeklyColumn} = require('../../../utils/data.js');

Page({
  data: {
    currentNav: 0,
    pageList: [
      {itemId: 0, itemName: 'LME基本金属开盘库存'},
      {itemId: 1, itemName: 'LME基本金属注销仓单'},
      {itemId: 2, itemName: 'COMEX铜库存'},
      {itemId: 3, itemName: '上期所仓单日报'},
      {itemId: 4, itemName: '上期所库存周报'},
    ],
    tradeDate: null,
    title: null,
    unit: null,
    tableInfo: {
      columns: [],
      data: []
    },
    columnsArray: [LMEStockColumns, LMECancelledColumns, COMEXColumns, SHFEDailyColumn, SHFEWeeklyColumn]
  },

  onLoad(options) {},

  onShow() {
    this.setData({
      currentIndex: 0,
      tradeDate: null,
      ['tableInfo.columns']: this.data.columnsArray[0]
    })
    this.getLmeStockFrontList()
  },
  // COMEX铜库存
  getComexFrontList(){
    let _tradeDate = this.data.tradeDate != null ? util.formatDate(this.data.tradeDate, 'YY-MM-DD') : '';
    QuoteApi.getComexFrontList({tradeDate: _tradeDate}).then(res=>{
      if (res.retCode == '000000' && res.errCode == '000000') {
        if (res.data.length > 0) {
          let array = res.data.map(item=>{
            item.metalType = 'cu';
            item.metalTypeName = '铜';
            return item;
          });
          if (!this.data.tradeDate) {
            let _title = util.formatDate(array[0].tradeDate, 'M月D日') + ' COMEX铜库存统计';
            let arr = [];
            array.forEach(item=>{
              if (item.tradeDate == array[0].tradeDate) {
                arr.push(item);
              }
            })
            this.setData({
              unit: '单位：短吨',
              title: _title,
              ['tableInfo.data']: arr
            })
          } else {
            this.setData({
              ['tableInfo.data']: array
            })
          }
        } else {
          this.setData({
            unit: '单位：短吨',
            title: 'COMEX铜库存统计',
            ['tableInfo.data']: []
          })
        }
      }
    })
  },
  // LME基本金属注销仓单
  getLmeWarehouseFrontList(){
    let _tradeDate = this.data.tradeDate != null ? util.formatDate(this.data.tradeDate, 'YY-MM-DD') : '';
    QuoteApi.getLmeWarehouseFrontList({tradeDate: _tradeDate}).then(res=>{
      if (res.retCode == '000000' && res.errCode == '000000') {
        if (res.data.length>0) {
          let arr = [];
          let sortArr = metalList;
          if (!this.data.tradeDate) {
            let _title = util.formatDate(res.data[0].tradeDate, 'M月D日') + ' LME基本金属注销仓单';
            res.data.forEach(item=>{
              if (item.tradeDate == res.data[0].tradeDate) {
                arr.push(item)
              }
            })
            this.setData({
              unit: '单位：吨',
              title: _title
            })
          }
          let searchArr = !this.data.tradeDate ? arr : res.data;
          let emptyObj = {changeRate: null, changeStockQuantity: null, changeStockRate: null, previousDayChangeRate: null, registerStockQuantity: null, registerStockRate: null, totalStockQuantity: null};
          let emptyKeys = Object.keys(emptyObj);
          sortArr.forEach(item => {
            emptyKeys.forEach(s=>{
                item[s] = emptyObj[s];
            })

            let obj = searchArr.find(i => i.metalType == item.metalType);
            if (obj) {
                item.metalTypeName = util.metalNameFilter(obj.metalType);
                item.changeRate = obj.changeRate;
                item.changeStockQuantity = obj.changeStockQuantity;
                item.changeStockRate = obj.changeStockRate;
                item.previousDayChangeRate = obj.previousDayChangeRate;
                item.registerStockQuantity = obj.registerStockQuantity;
                item.registerStockRate = obj.registerStockRate;
                item.totalStockQuantity = obj.totalStockQuantity;
            }
          })
          this.setData({
            ['tableInfo.data']: sortArr
          })
        } else {
          this.setData({
            unit: '单位：吨',
            title: 'LME基本金属注销仓单',
            ['tableInfo.data']: []
          })
        }
      }
    })
  },
  // LME基本金属库存报告
  getLmeStockFrontList(){
    let _tradeDate = this.data.tradeDate != null ? util.formatDate(this.data.tradeDate, 'YY-MM-DD') : '';
    QuoteApi.getLmeStockFrontList({tradeDate: _tradeDate}).then(res=>{
      if (res.retCode == '000000' && res.errCode == '000000') {
        if (res.data.length>0) {
          let arr = [];
          let sortArr = metalList;
          if (!this.data.tradeDate) {
            let _title = util.formatDate(res.data[0].tradeDate, 'M月D日') + ' LME基本金属官方开盘库存报告';
            res.data.forEach(item=>{
              if (item.tradeDate == res.data[0].tradeDate) {
                arr.push(item)
              }
            })
            this.setData({
              unit: '单位：吨',
              title: _title
            })
          }
          let searchArr = !this.data.tradeDate ? arr : res.data;
          let emptyObj = { changeStockQuantity: null, previousDayStockQuantity: null, totalStockQuantity: null };
          let emptyKeys = Object.keys(emptyObj);
          sortArr.forEach(item => {
            emptyKeys.forEach(s=>{
              item[s] = emptyObj[s];
            })

            let obj = searchArr.find(i => i.metalType == item.metalType);
            if (obj) {
              item.metalTypeName = util.metalNameFilter(obj.metalType);
              item.changeStockQuantity = obj.changeStockQuantity;
              item.previousDayStockQuantity = obj.previousDayStockQuantity;
              item.totalStockQuantity = obj.totalStockQuantity;
            }
          })
          this.setData({
            ['tableInfo.data']: sortArr
          })
        } else {
          this.setData({
            unit: '单位：吨',
            title: 'LME基本金属官方开盘库存报告',
            ['tableInfo.data']: []
          })
        }
      }
    })
  },
  // 上期所仓单日报
  getShfeWarehouseFrontList(){
    let _tradeDate = this.data.tradeDate != null ? util.formatDate(this.data.tradeDate, 'YY-MM-DD') : '';
    QuoteApi.getShfeWarehouseFrontList({tradeDate: _tradeDate}).then(res=>{
      if (res.retCode == '000000' && res.errCode == '000000') {
        if (res.data.length>0) {
          let arr = [];
          let sortArr = metalList_daily;
          if (!this.data.tradeDate) {
            let _title = util.formatDate(res.data[0].tradeDate, 'M月D日') + ' 上期所仓单日报';
            res.data.forEach(item=>{
              if (item.tradeDate == res.data[0].tradeDate) {
                arr.push(item)
              }
            })
            this.setData({
              unit: '单位：吨',
              title: _title
            })
          }
          let searchArr = !this.data.tradeDate ? arr : res.data;
          let emptyObj = { changeStockQuantity: null, previousDayStockQuantity: null, registerStockQuantity: null };
          let emptyKeys = Object.keys(emptyObj);
          sortArr.forEach(item => {
            emptyKeys.forEach(s=>{
              item[s] = emptyObj[s];
            })

            let obj = searchArr.find(i => i.metalType == item.metalType);
            if (obj) {
              item.metalTypeName = util.metalNameFilter(obj.metalType);
              item.changeStockQuantity = obj.changeStockQuantity;
              item.previousDayStockQuantity = obj.previousDayStockQuantity;
              item.registerStockQuantity = obj.registerStockQuantity;
            }
          })
          this.setData({
            ['tableInfo.data']: sortArr
          })
        } else {
          this.setData({
            unit: '单位：吨',
            title: '上期所仓单日报',
            ['tableInfo.data']: []
          })
        }
      }
    })
  },
  // 上期所库存周报
  getShfeStockFrontList(){
    let _tradeDate = this.data.tradeDate != null ? util.formatDate(this.data.tradeDate, 'YY-MM-DD') : '';
    QuoteApi.getShfeStockFrontList({tradeDate: _tradeDate}).then(res=>{
      if (res.retCode == '000000' && res.errCode == '000000') {
        if (res.data.length>0) {
          let arr = [];
          let sortArr = metalList_weekly;
          if (!this.data.tradeDate) {
            let _title = util.formatDate(res.data[0].tradeDate, 'M月D日') + ' 上期所库存周报';
            res.data.forEach(item=>{
              if (item.tradeDate == res.data[0].tradeDate) {
                arr.push(item)
              }
            })
            this.setData({
              unit: '单位：吨',
              title: _title
            })
          }
          let searchArr = !this.data.tradeDate ? arr : res.data;
          let emptyObj = { changeCapacityQuantity: null, changeFuturesQuantity: null, changeStockQuantity: null, lastCapacityQuantity: null,
            lastTotalFuturesQuantity: null, lastTotalStockQuantity: null, totalCapacityQuantity: null, totalFuturesQuantity: null, totalStockQuantity: null };
          let emptyKeys = Object.keys(emptyObj);
          sortArr.forEach(item => {
            emptyKeys.forEach(s=>{
              item[s] = emptyObj[s];
            })

            let obj = searchArr.find(i => i.metalType == item.metalType);
            if (obj) {
              item.metalTypeName = util.metalNameFilter(obj.metalType);
              item.changeCapacityQuantity = obj.changeCapacityQuantity;
              item.changeFuturesQuantity = obj.changeFuturesQuantity;
              item.changeStockQuantity = obj.changeStockQuantity;
              item.lastCapacityQuantity = obj.lastCapacityQuantity;
              item.lastTotalFuturesQuantity = obj.lastTotalFuturesQuantity;
              item.lastTotalStockQuantity = obj.lastTotalStockQuantity;
              item.totalCapacityQuantity = obj.totalCapacityQuantity;
              item.totalFuturesQuantity = obj.totalFuturesQuantity;
              item.totalStockQuantity = obj.totalStockQuantity;
            }
          })
          this.setData({
            ['tableInfo.data']: sortArr
          })
        } else {
          this.setData({
            unit: '单位：吨',
            title: '上期所库存周报',
            ['tableInfo.data']: []
          })
        }
      }
    })
  },
  menuClick(e){
    let current = e.currentTarget.dataset.current;

    this.setData({
      currentNav: current,
      tradeDate: null,
      ['tableInfo.columns']: this.data.columnsArray[current],
      ['tableInfo.data']: [],
      unit: null,
      title: null
    })
    if (Number(current) == 0) {
      this.getLmeStockFrontList()
    } else if (Number(current) == 1) {
      this.getLmeWarehouseFrontList();
    } else if (Number(current) == 2) {
      this.getComexFrontList();
    } else if (Number(current) == 3) {
      this.getShfeWarehouseFrontList();
    } else if (Number(current) == 4) {
      this.getShfeStockFrontList();
    }
  },
  // 日期选择变化
  bindDateChange: function(e) {
    this.setData({
      tradeDate: e.detail.value
    })
    if (Number(this.data.currentNav) == 0) {
      let _title = util.formatDate(e.detail.value, 'M月D日') + " LME基本金属官方开盘库存报告";
      this.setData({
        unit: "单位：吨",
        title: _title
      })
      this.getLmeStockFrontList()
    } else if (Number(this.data.currentNav) == 1) {
      let _title = util.formatDate(e.detail.value, 'M月D日') + " LME基本金属注销仓单";
      this.setData({
        unit: "单位：吨",
        title: _title
      })
      this.getLmeWarehouseFrontList()
    } else if (Number(this.data.currentNav) == 2) {
      let _title = util.formatDate(e.detail.value, 'M月D日') + " COMEX铜库存统计";
      this.setData({
        unit: "单位：短吨",
        title: _title
      })
      this.getComexFrontList()
    } else if (Number(this.data.currentNav) == 3) {
      let _title = util.formatDate(e.detail.value, 'M月D日') + " 上期所仓单日报";
      this.setData({
        unit: "单位：吨",
        title: _title
      })
      this.getShfeWarehouseFrontList()
    } else if (Number(this.data.currentNav) == 4) {
      let _title = util.formatDate(e.detail.value, 'M月D日') + " 上期所库存周报";
      this.setData({
        unit: "单位：吨",
        title: _title
      })
      this.getShfeStockFrontList()
    }
  },
})